﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2011 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "TestSuite.h"

LoggerManager* gLoggerManager = NULL;
DialectEnumerator* gDialectEnumerator = NULL;

//-----------------------------------------------------------------------
GlobalConfig::GlobalConfig(void)
{
	std::cout << "test setup" << std::endl;

	if (LoggerManager::getSingletonPtr() == NULL)
	{
		gLoggerManager = CELL_NEW LoggerManager();
		gLoggerManager->createLogger("test_primitive.log", true, true);
	}

	if (DialectEnumerator::getSingletonPtr() == NULL)
	{
		gDialectEnumerator = CELL_NEW DialectEnumerator();
	}
}
//-----------------------------------------------------------------------
GlobalConfig::~GlobalConfig(void)
{
	std::cout << "test teardown" << std::endl;

	if (gDialectEnumerator != NULL)
	{
		CELL_DELETE gDialectEnumerator;
		gDialectEnumerator = NULL;
	}

	if (gLoggerManager != NULL)
	{
		CELL_DELETE gLoggerManager;
		gLoggerManager = NULL;
	}
}


//-----------------------------------------------------------------------
SerializeTester::SerializeTester(void)
{
}
//-----------------------------------------------------------------------
SerializeTester::~SerializeTester(void)
{
}
//-----------------------------------------------------------------------
void SerializeTester::testSerialize(void)
{
	Primitive primitive;
	SubjectStuff subject("cellcloud");
	PredicateStuff predicate(int(32));
	primitive.commit(subject);
	primitive.commit(predicate);

	// 测试写入到序列化流
	SerializerStream stream;
	primitive.write(stream);

	String buf;
	stream.str(buf);
	BOOST_CHECK_EQUAL("[01.00]{sub=cellcloud:string}{pre=32:int}", buf);
	stream.free();

	// 测试从序列化流读出
	Primitive newPrimitive;
	stream << "[01.00]{sub=cellcloud:string}{obj=12:int}";
	newPrimitive.read(stream);

	BOOST_CHECK_EQUAL(1, newPrimitive.subjects()->size());
	BOOST_CHECK_EQUAL(1, newPrimitive.objectives()->size());
	BOOST_CHECK_EQUAL(0, newPrimitive.predicates()->size());

	BOOST_CHECK_EQUAL(Stuff::ST_SUBJECT, (*newPrimitive.subjects())[0].getType());
	BOOST_CHECK_EQUAL(Stuff::ST_OBJECTIVE, (*newPrimitive.objectives())[0].getType());

	
	BOOST_CHECK_EQUAL(LB_STRING, (*newPrimitive.subjects())[0].getLiteralBase());
	BOOST_CHECK_EQUAL(LB_INT, (*newPrimitive.objectives())[0].getLiteralBase());

	BOOST_CHECK_EQUAL("cellcloud", (*newPrimitive.subjects())[0].getValueAsString());
	BOOST_CHECK_EQUAL(12, (*newPrimitive.objectives())[0].getValueAsInt());
}
//-----------------------------------------------------------------------
void SerializeTester::testEscapeSerialize(void)
{
	Primitive primitive;
	SubjectStuff subject("cell [cloud], I'm {super} man :)= just a test");
	primitive.commit(subject);

	SerializerStream stream;
	primitive.write(stream);

	String buf;
	stream.str(buf);
	BOOST_CHECK_EQUAL("[01.00]{sub=cell [cloud], I'm \\{super\\} man \\:)\\= just a test:string}", buf);
	stream.free();
}
//-----------------------------------------------------------------------
void SerializeTester::testActionDialect(void)
{
	ActionDialect* dialect = dynamic_cast<ActionDialect*>(DialectEnumerator::getSingleton().createDialect(
		ActionDialectFactory::FACTORY_NAME, "xujiangwei"));

	dialect->setAction("SetName");
	dialect->appendParam("name", "Ambrose");

	Primitive primitive;
	dialect->translate(primitive);

	// 测试写入流
	SerializerStream stream;
	primitive.write(stream);

	String buf;
	stream.str(buf);
	BOOST_CHECK_EQUAL("[01.00]{sub=name:string}{pre=SetName:string}{obj=Ambrose:string}[ActionDialect@xujiangwei]", buf);
	stream.free();

	// 测试读取流
	Primitive result;
	stream << buf;
	result.read(stream);

	Dialect* rd = result.getDialect();

	BOOST_CHECK_EQUAL(ActionDialectFactory::FACTORY_NAME, rd->getName());
	BOOST_CHECK_EQUAL("SetName", (*result.predicates())[0].getValueAsString());
	BOOST_CHECK_EQUAL("name", (*result.subjects())[0].getValueAsString());
	BOOST_CHECK_EQUAL("Ambrose", (*result.objectives())[0].getValueAsString());

	DialectEnumerator::getSingleton().destroyDialect(dialect);
}



//-----------------------------------------------------------------------
PrimitiveTestSuite::PrimitiveTestSuite(void)
	: test_suite("PrimitiveTestSuite")
{
	// add member function test cases to a test suite
	boost::shared_ptr<SerializeTester> instance(new SerializeTester());

	test_case* testSerialize = BOOST_CLASS_TEST_CASE(&SerializeTester::testSerialize, instance);
	test_case* testEscapeSerialize = BOOST_CLASS_TEST_CASE(&SerializeTester::testEscapeSerialize, instance);
	test_case* testActionDialect = BOOST_CLASS_TEST_CASE(&SerializeTester::testActionDialect, instance);

	add(testSerialize);
	add(testEscapeSerialize);
	add(testActionDialect);
}
//-----------------------------------------------------------------------
PrimitiveTestSuite::~PrimitiveTestSuite(void)
{
}
